<?php
namespace app\api\controller;
use think\Controller;
use app\api\model\User;
use app\api\model\Wxuser as Wx;
class Login extends Controller{
    public function login(){
        $account = input('account');
        $root    = input('root');
        $user = new User;
        $results = $user->login($account,$root);
        if($results==1){
            AjaxReturn('1','登录成功',['results'=>$results]);
        }elseif ($results==2){
            AjaxReturn('10003','账号不存在，请前往注册');
        }elseif($results==3){
            AjaxReturn('10005','密码错误');
        }else{
            AjaxReturn('10004','系统错误，登陆失败');
        }
    }
    public function wxlogin(){
        //声明CODE，获取小程序传过来的CODE
        $code = $_GET['code'];
        $userInfo = $_GET['userInfo'];
        $avatarUrl = $_GET['avatarUrl'];
        //配置appid
        $appid = 'wxb9e25f5e185538f3';
        //配置appscret
        $secret = 'ca50d6d3ed31f42cf3d2a6f87c3559b0';
        //api接口
        $api = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
        //获取GET请求
        $results = file_get_contents($api);
        $js = json_decode($results,true);
        $openidd = $js['openid'];
        $wx = new Wx;
        $openid = $wx->findopenid($openidd);
        if(!$openid){
            $user = new User;
            $zc = $user->wxlogin($js,$userInfo,$avatarUrl);
            if($zc){
                AjaxReturn('1','注册并登录成功',['results'=>$js]);
            }else{
                AjaxReturn('77777','注册并登录失败');
            }
        }else{
            $user = new User;
            $dl = $user->wxdllogin($js);
            if($dl){
                AjaxReturn('1','登录成功',['results'=>$js]);
            }else{
                AjaxReturn('77776','登录失败');
            }
        }
    }
}